package com.ztesoft.web.baseconfig.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.web.baseconfig.db.po.ColumnNamePO;
import com.ztesoft.web.baseconfig.db.po.DmSrcTablePO;
import com.ztesoft.web.baseconfig.db.po.DmStrategyPO;
import com.ztesoft.web.baseconfig.db.po.DmTargetTablePO;

/**
 * <Description> <br>
 * 
 * @author lin.jie<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2015年8月20日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.baseconfig.service <br>
 */

public interface IDmSrcTableService {

	DmSrcTablePO selectByPrimaryKey(Integer key) throws BaseAppException;

	/**
	 * 获取所有字段信息
	 * 
	 * @param dbBaseId
	 * @param dbUserName
	 * @param tableName
	 * @return
	 * @throws BaseAppException
	 */
	ColumnNamePO[] selectAllColumns(Integer dbBaseId, String dbUserName, String tableName) throws BaseAppException;

	/**
	 * 根据表名，在本地已经配置好的数据库中，查找
	 * 
	 * @param record
	 * @return
	 * @throws BaseAppException
	 */
	List<DmSrcTablePO> selectByTableName(DmSrcTablePO record) throws BaseAppException;

	/**
	 * 根据表名查询源表信息
	 * 
	 * @param record
	 * @return
	 * @throws BaseAppException
	 */
	DmSrcTablePO selectByTableNameForShow(DmSrcTablePO record) throws BaseAppException;

	/**
	 * 表名的模糊查询
	 * 
	 * @param tableName
	 * @return
	 * @throws BaseAppException
	 */
	List<DmSrcTablePO> searchTableNameForSelect(String term) throws BaseAppException;

	List<DmSrcTablePO> selectByArg(DmSrcTablePO record) throws BaseAppException;

	List<DmSrcTablePO> selectSubTableBySourceDataId(Integer id) throws BaseAppException;

	Page<DmSrcTablePO> selectByArgAndPage(DmSrcTablePO record, Page<DmSrcTablePO> resultPage) throws BaseAppException;

	/**
	 * 验证表名是否存在，返回字段
	 * 
	 * @param record
	 *            :查询内容， isSubTable：是否是分表，hasSubTable：是否有分表（查询按钮 or
	 *            验证），isRead:是否是界面读取
	 * @return
	 * @throws BaseAppException
	 */
	ColumnNamePO[] selectTableName(DmSrcTablePO record, boolean isSubTable, boolean hasSubTable, boolean isRead)
			throws BaseAppException;

	/**
	 * 查找主键字段
	 * 
	 * @param record
	 * @return
	 * @throws BaseAppException
	 */
	String selectPkColumns(Integer dbBaseId, String dbUserName, String tableName) throws BaseAppException, Exception;

	/**
	 * 脚本验证，返回sql语句
	 * 
	 * @param record
	 * @return
	 * @throws BaseAppException
	 */
	String scriptVerify(DmTargetTablePO dttPO, DmSrcTablePO dstPO) throws BaseAppException, Exception;

	DmSrcTablePO add(DmSrcTablePO record) throws BaseAppException, Exception;

	int update(DmSrcTablePO record) throws BaseAppException;

	int delete(DmSrcTablePO record) throws BaseAppException;

	/**
	 * 根据策略标识获取对象信息
	 * 
	 * @param dmStrategyId
	 * @return
	 * @throws BaseAppException
	 * @throws Exception 
	 */
	DmSrcTablePO selectByDmStrategyId(DmStrategyPO po) throws Exception;

	/**
	 * 根据策略标识获取对象信息
	 * 
	 * @param dmStrategyId
	 * @return
	 * @throws BaseAppException
	 */
	DmSrcTablePO selectBydmStrategyId(Integer dmStrategyId) throws BaseAppException;

	/**
	 * 根据Form表单里的条件返回dmPlanId,草稿箱
	 * 
	 * @param record
	 * @return
	 * @throws BaseAppException
	 */
	List<DmSrcTablePO> selectByArgFormDraft(DmSrcTablePO record) throws BaseAppException;

	/**
	 * 根据Form表单里的条件返回dmPlanId,计划维护
	 * 
	 * @param record
	 * @return
	 * @throws BaseAppException
	 */
	List<DmSrcTablePO> selectByArgFormMaint(DmSrcTablePO record) throws BaseAppException;

	/**
	 * 获取单条记录长度
	 * 
	 * @param dbLinkId
	 * @param dbUserName
	 * @param tableName
	 * @return
	 * @throws BaseAppException
	 * @throws SQLException
	 */
	int getRecordSize(Integer dbLinkId, String dbUserName, String tableName) throws BaseAppException, SQLException;

	/**
	 * 查找字段类型
	 * 
	 * @param dbBaseId
	 * @param dbUserName
	 * @param tableName
	 * @return map(key为字段名，value为字段类型)
	 * @throws BaseAppException
	 * @throws Exception
	 */
	Map<String, Integer> findDataType(Integer dbBaseId, String dbUserName, String tableName) throws BaseAppException,
			Exception;

	/**
	 * 查询存储过程
	 * @param dbBaseId
	 * @param dbUserName
	 * @return
	 * @throws BaseAppException
	 * @throws Exception
	 */
	public List<DmSrcTablePO> findProcedureInfo(Integer dbBaseId, String dbUserName)
			throws BaseAppException, Exception;
	
	public  Map<String, String> qryInfoByDbLinkId(Integer dbBaseId, String dbUserName, String tableName)
			throws BaseAppException, Exception;
}
